package com.mf.morefun.ui.myclass;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.mf.morefun.R;
import com.mf.morefun.adapter.CreateClassHomeWorkAdapter;
import com.mf.morefun.base.BaseBusActivity;
import com.mf.morefun.bean.ArrangeItemBean;
import com.mf.morefun.bean.ClassListBean;
import com.mf.morefun.bean.DoHomeWorkBean;
import com.mf.morefun.bean.DoHomeWorkListBean;
import com.mf.morefun.bean.ResultBean;
import com.mf.morefun.constant.Configuration;
import com.mf.morefun.requestbean.RequestCreateClassHomeWorkBean;
import com.mf.morefun.requestbean.RequestEditClassHomeWorkBean;
import com.mf.morefun.requestbean.RequestHomeWorkItemBean;
import com.mf.morefun.requestbean.RequestUserBean;
import com.mf.morefun.ui.grindingears.GrindingEarsActivity;
import com.mf.morefun.ui.moreread.ListenRecordActivity;
import com.mf.morefun.ui.moreread.MoreReadingActivity;
import com.mf.morefun.widget.BackIdeaPop;
import com.mf.morefun.widget.NetErrorPop;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhcx.zhcxlibrary.utils.AppManager;
import com.zhcx.zhcxlibrary.utils.StringUtils;
import com.zhcx.zhcxlibrary.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CreateClassHomeWorkActivity extends BaseBusActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.iv_add)
    ImageView ivAdd;
    @BindView(R.id.iv_delete)
    ImageView ivDelete;
    @BindView(R.id.iv_submit)
    ImageView ivSubmit;
    @BindView(R.id.gv_createhomework)
    GridView gvCreatehomework;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private String type = "";
    private ClassListBean mClassListBean;
    private ClassicsHeader mClassicsHeader = null;
    private int pageNo = 1;
    private int totlePage = 1;
    private List<DoHomeWorkListBean> mHomeWorkList = new ArrayList<>();
    private CreateClassHomeWorkAdapter adapter;


    @Override
    protected int getNaviteColor() {
        return 0;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_createclasshomework;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        if (StringUtils.isEmpty(getIntent().getStringExtra("name"))) {
            tvName.setText("创建作业");
        } else {
            tvName.setText("创建作业(" + getIntent().getStringExtra("name") + ")");
        }
        IntentFilter mFilter = new IntentFilter();
        mFilter.addAction("HOMEWORKDATA");
        mFilter.addAction("DELETECLASSSIMPLEHOMEWORK");
        mFilter.addAction("SUBMITHOMEWORK");
        registerReceiver(mReceiver, mFilter);
        if (!StringUtils.isEmpty(getIntent().getStringExtra("HOMEWORKID")) && !StringUtils.isEmpty(getIntent().getStringExtra("CLASSID"))) {
            initGetList(getIntent().getStringExtra("HOMEWORKID"), getIntent().getStringExtra("CLASSID"), true);
        }
        mClassicsHeader = (ClassicsHeader) refreshLayout.getRefreshHeader();
        int delda = new Random().nextInt(7 * 24 * 60 * 60 * 1000);
        mClassicsHeader.setLastUpdateTime(new Date(System.currentTimeMillis() - delda));
        mClassicsHeader.setTimeFormat(new SimpleDateFormat("更新于 MM-dd HH:mm", Locale.CHINA));
        refreshLayout.getLayout().bringChildToFront(gvCreatehomework);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNo = 1;
                if (!StringUtils.isEmpty(getIntent().getStringExtra("HOMEWORKID")) && !StringUtils.isEmpty(getIntent().getStringExtra("CLASSID"))) {
                    initGetList(getIntent().getStringExtra("HOMEWORKID"), getIntent().getStringExtra("CLASSID"), true);
                }else {
                    mHandler.sendEmptyMessage(100);
                }
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNo++;
                if (pageNo < totlePage) {
                    if (!StringUtils.isEmpty(getIntent().getStringExtra("HOMEWORKID")) && !StringUtils.isEmpty(getIntent().getStringExtra("CLASSID"))) {
                        initGetList(getIntent().getStringExtra("HOMEWORKID"), getIntent().getStringExtra("CLASSID"), true);
                    }else {
                        mHandler.sendEmptyMessage(100);
                    }
                } else {
                    if (null != refreshLayout) {
                        refreshLayout.finishLoadMore();
                        refreshLayout.finishRefresh();
                        refreshLayout.setEnableLoadMore(false);
                    }
                }
            }
        });
        adapter = new CreateClassHomeWorkAdapter(CreateClassHomeWorkActivity.this, mHomeWorkList);
        gvCreatehomework.setAdapter(adapter);
        adapter.setSelectPosition(0);
        gvCreatehomework.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (null != adapter){
                    adapter.setSelectPosition(position);
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    private void initGetList(String homeworkid, String classid, boolean isRefresh) {
        RequestParams params = new RequestParams(Configuration.HOST + Configuration.SIMPLEARRANGELIST);
        RequestHomeWorkItemBean requestHomeWorkItemBean = new RequestHomeWorkItemBean();
        requestHomeWorkItemBean.setPageNo(pageNo);
        requestHomeWorkItemBean.setPageSize(20);
        requestHomeWorkItemBean.setOrderBy("tplrr.create_date desc");
        RequestHomeWorkItemBean.MfTpPageDataBean mfTpPageDataBean = new RequestHomeWorkItemBean.MfTpPageDataBean();
        mfTpPageDataBean.setClassId(classid);
        RequestHomeWorkItemBean.MfTpPageDataBean.MfTpLRRBean mfTpLRRBean = new RequestHomeWorkItemBean.MfTpPageDataBean.MfTpLRRBean();
        RequestHomeWorkItemBean.MfTpPageDataBean.MfTpLRRBean.TpDetailBean tpDetailBean = new RequestHomeWorkItemBean.MfTpPageDataBean.MfTpLRRBean.TpDetailBean();
        tpDetailBean.setId(homeworkid);
        mfTpLRRBean.setTpDetail(tpDetailBean);
        mfTpPageDataBean.setMfTpLRR(mfTpLRRBean);
        requestHomeWorkItemBean.setMfTpPageData(mfTpPageDataBean);
        params.setBodyContent(JSON.toJSONString(requestHomeWorkItemBean));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (null != refreshLayout) {
                    refreshLayout.finishLoadMore();
                    refreshLayout.finishRefresh();
                }
                if (isRefresh){
                    mHomeWorkList.clear();
                }
                if (!StringUtils.isEmpty(result)) {
                    ResultBean resultBean = JSON.parseObject(result, ResultBean.class);
                    if (resultBean.getErrorCode() == 0) {
                        if (!StringUtils.isEmpty(resultBean.getResult())) {
                            DoHomeWorkBean doHomeWorkBean = JSON.parseObject(resultBean.getResult(), DoHomeWorkBean.class);
                            totlePage = doHomeWorkBean.getTotalPage();
                            if (!StringUtils.isEmpty(doHomeWorkBean.getList())) {
                                List<DoHomeWorkListBean> homeWorkList = JSON.parseArray(doHomeWorkBean.getList(), DoHomeWorkListBean.class);
                                mHomeWorkList.addAll(homeWorkList);
                            }
                        }
                    }
                }
                mHandler.sendEmptyMessage(100);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                if (null != refreshLayout) {
                    refreshLayout.finishLoadMore();
                    refreshLayout.finishRefresh();
                }
                mHandler.sendEmptyMessage(101);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("HOMEWORKDATA")) {
                try {
                    AppManager.getAppManager().finishActivity(MoreReadingActivity.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                type = intent.getStringExtra("type");
                DoHomeWorkListBean.MfTpLRRBean mfTpLRRBean = new DoHomeWorkListBean.MfTpLRRBean();
                mfTpLRRBean.setName(intent.getStringExtra("BOOKNAME"));
                mfTpLRRBean.setClassId(getIntent().getStringExtra("CLASSID"));
                if (type.equals("2")){
                    mfTpLRRBean.setType("3");
                }else if(type.equals("3")){
                    mfTpLRRBean.setType("4");
                }
                DoHomeWorkListBean.MfTpLRRBean.BookBean bookBean = new DoHomeWorkListBean.MfTpLRRBean.BookBean();
                bookBean.setId(intent.getStringExtra("BOOKID"));
                bookBean.setCover(intent.getStringExtra("BOOKIMAGE"));
                mfTpLRRBean.setBook(bookBean);
                DoHomeWorkListBean.MfTpLRRBean.ListenBean listenBean = new DoHomeWorkListBean.MfTpLRRBean.ListenBean();
                listenBean.setId(intent.getStringExtra("LISTENID"));
                mfTpLRRBean.setListen(listenBean);
                DoHomeWorkListBean doHomeWorkListBean = new DoHomeWorkListBean();
                doHomeWorkListBean.setMfTpLRR(mfTpLRRBean);
                mHomeWorkList.add(doHomeWorkListBean);
                mHandler.sendEmptyMessage(100);
            }else if(intent.getAction().equals("DELETECLASSSIMPLEHOMEWORK")){
                if (null != mHomeWorkList.get(intent.getIntExtra("POSITION", 0)).getMfTpLRR().getTpDetail()){
                    if (StringUtils.isEmpty(mHomeWorkList.get(intent.getIntExtra("POSITION", 0)).getMfTpLRR().getTpDetail().getId())){
                        mHomeWorkList.remove(intent.getIntExtra("POSITION", 0));
                        mHandler.sendEmptyMessage(100);
                    }else {
                        initDeleteSimleHomeWork(intent.getIntExtra("POSITION", 0));
                    }
                }else {
                    mHomeWorkList.remove(intent.getIntExtra("POSITION", 0));
                    mHandler.sendEmptyMessage(100);
                }
            }else if(intent.getAction().equals("SUBMITHOMEWORK")){
                if (StringUtils.isEmpty(getIntent().getStringExtra("HOMEWORKID"))){
                    initSubmit();
                }else {
                    initEditSubmit();
                }
            }
        }
    };

    private void initDeleteSimleHomeWork(int position) {
        RequestParams params = new RequestParams(Configuration.HOST + Configuration.DELETESIMPLEHOMEWORK);
        RequestUserBean requestUserBean = new RequestUserBean();
        requestUserBean.setId(mHomeWorkList.get(position).getMfTpLRR().getId());
        params.setBodyContent(JSON.toJSONString(requestUserBean));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (!StringUtils.isEmpty(result)){
                    ResultBean mResult = JSON.parseObject(result, ResultBean.class);
                    if (mResult.getErrorCode() == 0){
                        mHomeWorkList.remove(position);
                        mHandler.sendEmptyMessage(100);
                    }else {
                        ToastUtils.show(getApplicationContext(), mResult.getMessage(), 100);
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                mHandler.sendEmptyMessage(101);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.iv_back, R.id.iv_add, R.id.iv_delete, R.id.iv_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                BackIdeaPop pop = new BackIdeaPop(CreateClassHomeWorkActivity.this);
                pop.showPop("确定退出当前页面吗？", 13, 0);
                break;
            case R.id.iv_add:
                try {
                    showPop();
                }catch (Exception e){
                    e.printStackTrace();
                }
                break;
            case R.id.iv_delete:
                //删除
                if (null != adapter){
                    BackIdeaPop pops = new BackIdeaPop(CreateClassHomeWorkActivity.this);
                    pops.showPop("确定要删除 "+ mHomeWorkList.get(adapter.getSelectPosition()).getMfTpLRR().getName() + "吗?", 9, adapter.getSelectPosition());
                }
                break;
            case R.id.iv_submit:
                //创建单次作业
                if (mHomeWorkList.size() > 0){
                    BackIdeaPop pops = new BackIdeaPop(CreateClassHomeWorkActivity.this);
                    pops.showPop("确定提交作业吗?", 12, 0);
                }else {
                    ToastUtils.show(getApplicationContext(), "请先添加作业",  100);
                }
                break;
        }
    }

    private void initEditSubmit() {
        RequestParams params = new RequestParams(Configuration.HOST + Configuration.EDITARRANGE);
        RequestEditClassHomeWorkBean requestCreateClassHomeWorkBean = new RequestEditClassHomeWorkBean();
        requestCreateClassHomeWorkBean.setId(getIntent().getStringExtra("HOMEWORKID"));
        requestCreateClassHomeWorkBean.setClassId(getIntent().getStringExtra("CLASSID"));
        requestCreateClassHomeWorkBean.setName(getIntent().getStringExtra("name"));
        List<RequestEditClassHomeWorkBean.MfTpLRRListBean> mList = new ArrayList<>();
        for (int i = 0; i < mHomeWorkList.size(); i++){
            RequestEditClassHomeWorkBean.MfTpLRRListBean mfTpLRRListBean = new RequestEditClassHomeWorkBean.MfTpLRRListBean();
            mfTpLRRListBean.setName(mHomeWorkList.get(i).getMfTpLRR().getName());
            mfTpLRRListBean.setType(mHomeWorkList.get(i).getMfTpLRR().getType());
            mfTpLRRListBean.setId(mHomeWorkList.get(i).getMfTpLRR().getId());
            if (mHomeWorkList.get(i).getMfTpLRR().getType().equals("3") && null != mHomeWorkList.get(i).getMfTpLRR().getListen()){
                RequestEditClassHomeWorkBean.MfTpLRRListBean.ListenBean listenBean = new RequestEditClassHomeWorkBean.MfTpLRRListBean.ListenBean();
                listenBean.setId(mHomeWorkList.get(i).getMfTpLRR().getListen().getId());
                mfTpLRRListBean.setListen(listenBean);
            }
            if (!mHomeWorkList.get(i).getMfTpLRR().getType().equals("5")){
                RequestEditClassHomeWorkBean.MfTpLRRListBean.BookBean bookBean = new RequestEditClassHomeWorkBean.MfTpLRRListBean.BookBean();
                bookBean.setId(mHomeWorkList.get(i).getMfTpLRR().getBook().getId());
                mfTpLRRListBean.setBook(bookBean);
            }else {
                RequestEditClassHomeWorkBean.TpCreation tpCreation = new RequestEditClassHomeWorkBean.TpCreation();
                tpCreation.setName(mHomeWorkList.get(i).getMfTpLRR().getName());
                tpCreation.setTextDescription(mHomeWorkList.get(i).getMfTpCreation().getTextDescription());
                mfTpLRRListBean.setTpCreation(tpCreation);
            }
            mList.add(mfTpLRRListBean);
        }
        requestCreateClassHomeWorkBean.setMfTpLRRList(mList);
        params.setBodyContent(JSON.toJSONString(requestCreateClassHomeWorkBean));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (!StringUtils.isEmpty(result)) {
                    ResultBean mResult = JSON.parseObject(result, ResultBean.class);
                    if (mResult.getErrorCode() == 0){
                        EventBus.getDefault().post("yes");
                        finish();
                    }else {
                        ToastUtils.show(getApplicationContext(), mResult.getMessage(), 100);
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                mHandler.sendEmptyMessage(101);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initSubmit() {
        RequestParams params = new RequestParams(Configuration.HOST + Configuration.CREATEHOMEWORK);
        RequestCreateClassHomeWorkBean requestCreateClassHomeWorkBean = new RequestCreateClassHomeWorkBean();
        requestCreateClassHomeWorkBean.setClassId(getIntent().getStringExtra("CLASSID"));
        requestCreateClassHomeWorkBean.setName(getIntent().getStringExtra("name"));
        List<RequestCreateClassHomeWorkBean.MfTpLRRListBean> mList = new ArrayList<>();
        for (int i = 0; i < mHomeWorkList.size(); i++){
            RequestCreateClassHomeWorkBean.MfTpLRRListBean mfTpLRRListBean = new RequestCreateClassHomeWorkBean.MfTpLRRListBean();
            mfTpLRRListBean.setName(mHomeWorkList.get(i).getMfTpLRR().getName());
            mfTpLRRListBean.setType(mHomeWorkList.get(i).getMfTpLRR().getType());
            if (mHomeWorkList.get(i).getMfTpLRR().getType().equals("3") && null != mHomeWorkList.get(i).getMfTpLRR().getListen()){
                RequestCreateClassHomeWorkBean.MfTpLRRListBean.ListenBean listenBean = new RequestCreateClassHomeWorkBean.MfTpLRRListBean.ListenBean();
                listenBean.setId(mHomeWorkList.get(i).getMfTpLRR().getListen().getId());
                mfTpLRRListBean.setListen(listenBean);
            }
            if (!mHomeWorkList.get(i).getMfTpLRR().getType().equals("5")){
                RequestCreateClassHomeWorkBean.MfTpLRRListBean.BookBean bookBean = new RequestCreateClassHomeWorkBean.MfTpLRRListBean.BookBean();
                bookBean.setId(mHomeWorkList.get(i).getMfTpLRR().getBook().getId());
                mfTpLRRListBean.setBook(bookBean);
            }else {
                RequestCreateClassHomeWorkBean.TpCreation tpCreation = new RequestCreateClassHomeWorkBean.TpCreation();
                tpCreation.setName(mHomeWorkList.get(i).getMfTpLRR().getName());
                tpCreation.setTextDescription(mHomeWorkList.get(i).getMfTpCreation().getTextDescription());
                mfTpLRRListBean.setTpCreation(tpCreation);
            }
            mList.add(mfTpLRRListBean);
        }
        requestCreateClassHomeWorkBean.setMfTpLRRList(mList);
        params.setBodyContent(JSON.toJSONString(requestCreateClassHomeWorkBean));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (!StringUtils.isEmpty(result)) {
                    ResultBean mResult = JSON.parseObject(result, ResultBean.class);
                    if (mResult.getErrorCode() == 0){
                        EventBus.getDefault().post("yes");
                        finish();
                    }else {
                        ToastUtils.show(getApplicationContext(), mResult.getMessage(), 100);
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                mHandler.sendEmptyMessage(101);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 101:
                    NetErrorPop errorPop = new NetErrorPop(CreateClassHomeWorkActivity.this);
                    errorPop.showPop("");
                    break;
                case 100:
                    if (null != adapter){
                        adapter.setSelectPosition(0);
                        adapter.notifyDataSetChanged();
                    }
                    break;
            }
        }
    };

    private PopupWindow mDoPop;

    //弹出操作的弹框   需要三个按钮   设置有效期， 重置密码， 删除用户
    private void showPop() {
        if (null == mDoPop) {
            View view = LayoutInflater.from(CreateClassHomeWorkActivity.this).inflate(R.layout.layout_create, null);
            RelativeLayout rl_bg = view.findViewById(R.id.rl_bg);
            rl_bg.getBackground().setAlpha(100);
            TextView tv_record = view.findViewById(R.id.tv_record);//修改名称
            tv_record.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDoPop.dismiss();
                    mDoPop = null;
                    Intent mIntent = new Intent(CreateClassHomeWorkActivity.this, MoreReadingActivity.class);
                    mIntent.putExtra("type", "2");
                    startActivity(mIntent);
                }
            });
            TextView tv_video = view.findViewById(R.id.tv_video);
            tv_video.setOnClickListener(new View.OnClickListener() {//修改图片
                @Override
                public void onClick(View v) {
                    mDoPop.dismiss();
                    mDoPop = null;
                    Intent mIntent = new Intent(CreateClassHomeWorkActivity.this, MoreReadingActivity.class);
                    mIntent.putExtra("type", "3");
                    startActivity(mIntent);
                }
            });
            TextView tv_creation = view.findViewById(R.id.tv_creation);
            tv_creation.setOnClickListener(new View.OnClickListener() {//删除班级
                @Override
                public void onClick(View v) {
                    mDoPop.dismiss();
                    mDoPop = null;
                    try {
                        showCreatePop();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            });
            TextView tvCancel = view.findViewById(R.id.tv_cancel);
            tvCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDoPop.dismiss();
                    mDoPop = null;
                }
            });
            mDoPop = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
            mDoPop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
            mDoPop.setBackgroundDrawable(new BitmapDrawable());
            mDoPop.setFocusable(true);
            mDoPop.update();
            mDoPop.showAtLocation(view, Gravity.CENTER, 0, 0);
            mDoPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    mDoPop.dismiss();
                    mDoPop = null;
                }
            });
        }
    }

    private PopupWindow mCreatePop;
    private void showCreatePop() {
        if (null == mCreatePop) {
            View view = LayoutInflater.from(CreateClassHomeWorkActivity.this).inflate(R.layout.layout_createpop, null);
            RelativeLayout rl_bg = view.findViewById(R.id.rl_bg);
            rl_bg.getBackground().setAlpha(100);
            EditText edit_name = view.findViewById(R.id.edit_name);
            EditText edit_decription = view.findViewById(R.id.edit_decription);
            TextView tvCancel = view.findViewById(R.id.tv_cancel);
            tvCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mCreatePop.dismiss();
                    mCreatePop = null;
                }
            });
            TextView tv_sure = view.findViewById(R.id.tv_sure);
            tv_sure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //添加
                    if (StringUtils.isEmpty(edit_name.getText().toString())){
                        ToastUtils.show(getApplicationContext(), "请输入名称", 100);
                        return;
                    }
                    if (StringUtils.isEmpty(edit_decription.getText().toString())){
                        ToastUtils.show(getApplicationContext(), "请输入说明", 100);
                        return;
                    }
                    mCreatePop.dismiss();
                    mCreatePop = null;
                    DoHomeWorkListBean.MfTpLRRBean mfTpLRRBean = new DoHomeWorkListBean.MfTpLRRBean();
                    mfTpLRRBean.setName(edit_name.getText().toString());
                    mfTpLRRBean.setClassId(getIntent().getStringExtra("CLASSID"));
                    mfTpLRRBean.setType("5");
                    DoHomeWorkListBean.MfTpCreation mfTpCreation = new DoHomeWorkListBean.MfTpCreation();
                    mfTpCreation.setName(edit_name.getText().toString());
                    mfTpCreation.setTextDescription(edit_decription.getText().toString());
                    DoHomeWorkListBean doHomeWorkListBean = new DoHomeWorkListBean();
                    doHomeWorkListBean.setMfTpLRR(mfTpLRRBean);
                    doHomeWorkListBean.setMfTpCreation(mfTpCreation);
                    mHomeWorkList.add(doHomeWorkListBean);
                    mHandler.sendEmptyMessage(100);
                }
            });
            mCreatePop = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
            mCreatePop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
            mCreatePop.setBackgroundDrawable(new BitmapDrawable());
            mCreatePop.setFocusable(true);
            mCreatePop.update();
            mCreatePop.showAtLocation(view, Gravity.CENTER, 0, 0);
            mCreatePop.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    mCreatePop.dismiss();
                    mCreatePop = null;
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            unregisterReceiver(mReceiver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
